<template>

  <div>

    <el-dialog :visible.sync="dialog" title="字段信息" append-to-body width="1500px" height="800px">
      <el-table
        :data="columns"
        size="mini"
        border
        style="width: 100%"
        highlight-current-row
      >
        <el-table-column label="字段名" align="center">
          <template slot-scope="scope">
            <el-input v-model="scope.row.javaField" size="mini" placeholder="请输入" />
          </template>
        </el-table-column>
        <el-table-column label="字段名(db)" align="center">
          <template slot-scope="scope">
            <el-input v-model="scope.row.name" size="mini" placeholder="" />
          </template>
        </el-table-column>
        <el-table-column label="长度" align="center">
          <template slot-scope="scope">
            <el-input-number v-model="scope.row.length" :min="1" :max="2000" style="width: 100px" controls-position="right" size="mini" />
          </template>
        </el-table-column>
        <el-table-column label="字段描述" align="center">
          <template slot-scope="scope">
            <el-input v-model="scope.row.comments" size="mini" placeholder="请输入" />
          </template>
        </el-table-column>
        <el-table-column label="java类型" align="center">
          <template slot-scope="scope">
            <el-select v-model="scope.row.javaType" clearable placeholder="请选择">
              <el-option
                v-for="item in javaTypes"
                :key="item"
                :label="item"
                :value="item"
              />
            </el-select>
          </template>
        </el-table-column>

        <el-table-column label="jdbc类型" align="center">
          <template slot-scope="scope">
            <el-select v-model="scope.row.jdbcType" clearable placeholder="请选择">
              <el-option
                v-for="item in jdbcTypes"
                :key="item"
                :label="item"
                :value="item"
              />
            </el-select>
          </template>
        </el-table-column>

        <el-table-column label="为空" align="center" width="55px">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.isNull"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-value="1"
              inactive-value="0"
            />
          </template>
        </el-table-column>

        <el-table-column label="更新" align="center" width="55px">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.isEdit"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-value="1"
              inactive-value="0"
            />
          </template>
        </el-table-column>

        <el-table-column label="列表" align="center" width="55px">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.isList"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-value="1"
              inactive-value="0"
            />
          </template>
        </el-table-column>

        <el-table-column label="查询" align="center" width="55px">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.isQuery"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-value="1"
              inactive-value="0"
            />

          </template>
        </el-table-column>

        <el-table-column label="查询方式" align="center">
          <template slot-scope="scope">
            <el-select v-model="scope.row.queryType" clearable placeholder="请选择">
              <el-option
                v-for="item in queryTypes"
                :key="item"
                :label="item"
                :value="item"
              />
            </el-select>
          </template>
        </el-table-column>

        <el-table-column label="显示方案" align="center">
          <template slot-scope="scope">
            <el-select v-model="scope.row.showType" clearable placeholder="请选择">
              <el-option
                v-for="item in showTypes"
                :key="item"
                :label="item"
                :value="item"
              />
            </el-select>
          </template>
        </el-table-column>

        <el-table-column label="字典类型" align="center">
          <template slot-scope="scope">
            <el-select v-model="scope.row.dictType" clearable placeholder="请选择">
              <el-option
                v-for="item in dicts"
                :key="item.key"
                :label="item.value"
                :value="item.key"
              />
            </el-select>
          </template>
        </el-table-column>

        <el-table-column label="排序" align="center">
          <template slot-scope="scope">
            <el-input-number v-model="scope.row.sort" :min="1" :max="100" style="width: 80px" controls-position="right" size="mini" />
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="70px">
          <template slot-scope="scope">
            <el-button slot="reference" type="danger" icon="el-icon-delete" size="mini" @click="del(scope.$index)" />
          </template>
        </el-table-column>
      </el-table>
      <div class="el-table-add-row" style="width: 100%;" @click="addRow"><span>+ 添加</span></div>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="cancel">取消</el-button>
        <el-button :loading="loading" type="primary" @click="doSubmit">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { get, update } from '@/api/generator/tablecolumn'
export default {
  props: {

    dicts: {
      type: Array,
      required: true
    }

  },
  data() {
    return {
      id: 0,
      loading: false, dialog: false,
      columns: [],
      queryTypes: ['=', '!=', 'bettewAnd', 'like'],
      showTypes: ['input', 'select', 'radio', 'checkbox', 'switch'],
      javaTypes: ['Integer', 'Long', 'String', 'Date', 'BigDecimal'],
      jdbcTypes: ['int', 'bigint', 'varchar', 'datetime', 'decimal']
    }
  },
  created() {

  },
  methods: {
    to() {
      get(this.id).then(res => {
        this.columns = res.data.columns
        if (this.columns.length === 0) {
          this.columns.push({ id: null, javaField: '', name: '', comments: '', jdbcType: '', javaType: '',
            isPk: '', isNull: '1', isEdit: '1', isList: '1', isQuery: '1', queryType: '', showType: '',
            dictType: '', sort: 1, length: 10, lztTableId: this.id
          })
        }
        this.dialog = true
      })
    },
    addRow() {
      var obj = { id: null, javaField: '', name: '', comments: '', jdbcType: '', javaType: '',
        isPk: '', isNull: '1', isEdit: '1', isList: '1', isQuery: '1', queryType: '', showType: '',
        dictType: '', sort: 1, length: 10, lztTableId: this.id
      }
      this.columns.push(obj)
    },
    cancel() {
      this.dialog = false
    },
    doSubmit() {
      this.loading = true
      update(this.columns).then(res => {
        this.$notify({
          title: '保存成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.dialog = false
      }).catch(err => {
        this.loading = false
        console.info(err)
      })
    },
    del(index) {
      this.columns.splice(index, 1)
    }
  }
}
</script>

<style scoped>
  .el-table-add-row {
    margin-top: 10px;
    width: 100%;
    height: 34px;
    border: 1px dashed #c1c1cd;
    border-radius: 3px;
    cursor: pointer;
    justify-content: center;
    display: flex;
    line-height: 34px;
  }
  div{
    display: inline-block;
    margin-right: 3px;
  }
</style>
